<?php

defined('SYSPATH') or die('Access error!');

class Controller_Shop_Seller_Store extends Controller_Shop_User_Main
{

    private $store;

    public function before()
    {
        parent::before();
        $this->store = Model::factory('shop_store');
        Assets::css('base', $this->template->media_path . 'css/base.css');
        I18n::set_current_file('shop');
        $this->template->assign('user_shop_nav', $this->user_shop_seller_nav());
    }

    //添加一个店铺
    public function action_base()
    {
        
        //当前位置
        $this->set_define_position(__('seller manager'));
        Breadcrumbs::add(__('store base'));
        
        //根据用户查找是否已经存在店铺
        $store_detail = $this->store->get_store_by_userid($this->user_detail['id']);
        if (Arr::validate_arr($store_detail))
        {
            $this->edit($store_detail['id']);
            $this->template->assign('data', $store_detail);
        }
        else
        {
            $this->add();
        }
        Assets::js('tinymce', $this->template->media_path . 'js/kindeditor/kindeditor-min.js');
        $this->template->assign('action', Route::get('shop/seller/store')->uri(array('action' => 'base')));
        $this->template->view('shop_store_base');
    }

    public function action_theme()
    {
        
        //当前位置
        $this->set_define_position(__('seller manager'));
        Breadcrumbs::add(__('store theme'));
        
        //获取用户的风格
        $store_theme = $this->store->get_var(array('user_id' => $this->user_detail['id']), 'theme');
        $this->template->assign('current_theme', $store_theme);
        /* 获得可用的模版 */
        $shop_themes = array();
        $template_dir = @opendir(SHOP_TEMPLATE_STYLE);
        while ($file = readdir($template_dir))
        {
            if ($file != '.' && $file != '..' && is_dir(SHOP_TEMPLATE_STYLE . $file) && $file != '.svn')
            {
                $shop_themes[] = $this->store->get_themes_info($file);
            }
        }
        @closedir($template_dir);
        $this->template->assign('shop_themes', $shop_themes);
        $this->template->view('shop_themes');
    }

    public function action_save_theme()
    {

        $data = array();
        $data['theme'] = Arr::get($_POST, 'theme', 'red');
        if ($this->store->update($data, array('user_id' => $this->user_detail['id'])))
        {
            Message::success(__('store themes update  success'));
            $this->request->redirect(Route::get('shop/seller/store')->uri(array('action' => 'theme')));
        }
    }

    public function add()
    {
        if ($_POST)
        {
            $_POST['add_time'] = Date::get_time();
            $this->save($_POST);
        }
    }

    public function edit($id)
    {
        if ($_POST)
        {
            $this->save($_POST, array('id' => $id));
        }
    }

    public function handle_post($data)
    {
        $data['user_id'] = $this->user_detail['id'];
        $data['username'] = $this->user_detail['username'];
        $data['description'] = Arr::get($data, 'description', '');
        $data['description'] = HTML::sanitize_content(htmlspecialchars_decode($data['description']));
        $data['update_time'] = Date::get_time();
        return parent::handle_post($data);
    }

    public function save($post, $where_arr = array())
    {
        $data = $this->handle_post($post);
        if (!$this->store->validate($data))
        {
            Message::error(__('validate error'));
            $this->request->redirect(Route::get('shop/seller/store')->uri(array('action' => 'base')));
        }
        $data['store_logo'] = $this->save_simple_file(SHOP_STORE_IMAGES_SAVE_PATH . $this->user_detail['id'] . '/', 'store_logo');
        if (empty($data['store_logo']))
        {
            unset($data['store_logo']);
        }

        $data['store_banner'] = $this->save_simple_file(SHOP_STORE_IMAGES_SAVE_PATH . $this->user_detail['id'] . '/', 'store_banner');
        if (empty($data['store_banner']))
        {
            unset($data['store_banner']);
        }
        if (Arr::validate_arr($where_arr))
        {
            if ($this->store->update($data, array('id' => $where_arr['id'])))
            {
                Message::success(__('update success'));
                $this->request->redirect(Route::get('shop/seller/store')->uri(array('action' => 'base', 'id' => $where_arr['id'])));
            }
        }
        else
        {
            if ($this->store->insert($data))
            {
                Message::success(__('store add success'));
                $this->request->redirect(Route::get('shop/seller/store')->uri(array('action' => 'base')));
            }
        }
    }

}